package com.adapters;

import java.util.ArrayList;

import android.content.Context;
import android.os.storage.OnObbStateChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.objects.ContactCheck;
import com.senditcertified.mail.R;
import com.views.ACheckBox;

public class ContactsAdapter extends BaseAdapter implements
		ACheckBox.OnCheckListener, OnClickListener {
	public static final int MODE_CHECK_ALL = 0;
	public static final int MODE_UNCHECK_ALL = 1;
	public static final int MODE_NORMAL = 3;
	private Context mContext;
	private ArrayList<ContactCheck> lst;
	private LayoutInflater inflater = null;
	private int checkMode = MODE_NORMAL;
	private ACheckBox mCheckAll;

	public ContactsAdapter(Context mContext,ACheckBox checkAll, ArrayList<ContactCheck> lst) {
		this.mContext = mContext;
		this.lst = lst;
		this.inflater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.mCheckAll=checkAll;

	}
	public String getContactChoosed(){
		String returnValue="";
		for (int i = 0; i < getCount(); i++) {
			ContactCheck check=getItem(i);
			if (check.isCheck()) {
				returnValue+=(check.getID()+";");
			}
		}
		return returnValue;
	}
	public int getCount() {
		if (lst == null)
			return 0;
		else
			return lst.size();
	}

	public void setCheckMode(int checkMode) {
		this.checkMode = checkMode;
	}

	public ContactCheck getItem(int position) {
		return lst.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
		ContactCheck item = getItem(position);
		if (convertView == null) {
			v = inflater.inflate(R.layout.item_contact, null);
		}
		TextView tvName = (TextView) v.findViewById(R.id.tv_name);
		TextView tvAddress = (TextView) v.findViewById(R.id.tv_address);
		TextView tvAction = (TextView) v.findViewById(R.id.tv_action);
		ACheckBox mCb = (ACheckBox) v.findViewById(R.id.checkbox);
		tvName.setText(item.getName());
		tvAddress.setText(item.getEmailAddress());
		tvAction.setText("View/Edit");
		if (checkMode == MODE_CHECK_ALL) {
			item.setCheck(true);
		} else if (checkMode == MODE_UNCHECK_ALL) {
			item.setCheck(false);
		}
		mCb.setChecked(item.isCheck());
		mCb.setTag(position);
		mCb.setOnCheckListener(this);
		tvAction.setTag(position);
		tvAction.setOnClickListener(this);
		tvName.setSelected(true);
		tvAddress.setSelected(true);
		tvAction.setSelected(true);

		return v;
	}

	@Override
	public void onCheck(View buttonView, boolean isChecked) {
		getItem(Integer.parseInt(buttonView.getTag().toString())).setCheck(
				isChecked);
		setCheckAll();

	}

	// process checkall for checkbox
	public void setCheckAll() {
		boolean check = true;
		for (int i = 0; i < getCount(); i++) {
			if (!getItem(i).isCheck()) {
				check = false;
				break;
			}
		}
		mCheckAll.setChecked(check);
	}

	public void onViewEdit(int pos) {

	}

	@Override
	public void onClick(View v) {
		onViewEdit(Integer.parseInt(v.getTag().toString()));
	}

}